package itsm.isperp.framework.data.mybatis.utils;

import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * Mbatis映射声明{@link org.apache.ibatis.mapping.MappedStatement}工具类
 * 
 * @author lizx
 * @date 2013-1-24
 * @version V1.0
 */
public final class MappedStatementUtil {

	/**
	 * 复制映射声明
	 * 
	 * @param ms
	 *            源映射声明
	 * @param newSqlSource
	 *            一个新的sql源
	 * @return 返回一个新的映射声明
	 */
	public static MappedStatement copyFromMappedStatement(MappedStatement ms,
			SqlSource newSqlSource) {
		MappedStatement.Builder builder = new MappedStatement.Builder(
				ms.getConfiguration(), ms.getId(), newSqlSource,
				ms.getSqlCommandType());
		builder.resource(ms.getResource());
		builder.fetchSize(ms.getFetchSize());
		builder.statementType(ms.getStatementType());
		builder.keyGenerator(ms.getKeyGenerator());
		if (ms.getKeyProperties() != null) {
			for (String keyProperty : ms.getKeyProperties()) {
				builder.keyProperty(keyProperty);
			}
		}

		builder.timeout(ms.getTimeout());
		builder.parameterMap(ms.getParameterMap());
		builder.resultMaps(ms.getResultMaps());
		builder.cache(ms.getCache());
		builder.resultSetType(ms.getResultSetType());
		builder.flushCacheRequired(ms.isFlushCacheRequired());
		builder.useCache(ms.isUseCache());

		return builder.build();
	}
}
